# 设置OpenAI的API密钥
import os

from langchain_openai import ChatOpenAI

os.environ["OPENAI_API_KEY"] = 'sk-f2M0jWxDEb7IJKiuCnMsvKE7TLSYCA36pyl8gWA1XLaHnD8l'
os.environ["OPENAI_BASE_URL"] = 'https://api.f2gpt.com/v1'

# 导入文档加载器模块，并使用TextLoader来加载文本文件
from langchain.document_loaders import TextLoader

loader = TextLoader('./OneFlower/花语大全.txt', encoding='utf8')

# 使用VectorstoreIndexCreator来从加载器创建索引
from langchain.indexes import VectorstoreIndexCreator
from langchain.embeddings import OpenAIEmbeddings


embeddings = OpenAIEmbeddings()
index = VectorstoreIndexCreator(embedding=embeddings).from_loaders([loader])
llm = ChatOpenAI(temperature=0, model='gpt-3.5-turbo')

# 定义查询字符串, 使用创建的索引执行查询
query = "玫瑰花的花语是什么？"
result = index.query(llm=llm, question=query)


# 替换成你所需要的工具
from langchain.text_splitter import CharacterTextSplitter

text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings

embeddings = OpenAIEmbeddings()
index_creator = VectorstoreIndexCreator(
    vectorstore_cls=Chroma,
    embedding=OpenAIEmbeddings(),
    text_splitter=CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
)

print(result)  # 打印查询结果